package cn.iocoder.yudao.module.student.controller.app.studentCurriculumVitae;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.studentCurriculumVitae.StudentCurriculumVitaeApi;
import cn.iocoder.yudao.module.member.api.studentCurriculumVitae.dto.StudentCurriculumVitaeCreateReqDTO;
import cn.iocoder.yudao.module.member.api.studentCurriculumVitae.dto.StudentCurriculumVitaePageReqDTO;
import cn.iocoder.yudao.module.member.api.studentCurriculumVitae.dto.StudentCurriculumVitaeRespDTO;
import cn.iocoder.yudao.module.member.api.studentCurriculumVitae.dto.StudentCurriculumVitaeUpdateReqDTO;
import cn.iocoder.yudao.module.member.api.studentEducationalExperience.dto.StudentEducationalExperienceCreateReqDTO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentCreateReqVO;
import cn.iocoder.yudao.module.student.controller.app.student.vo.WebStudentInfoRespVO;
import cn.iocoder.yudao.module.student.service.student.WebStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -学生端- 用户简历")
@RestController
@RequestMapping("/student/student-curriculum-vitae")
@Validated
@Slf4j
public class WebStudentCurriculumVitaeController {

    @Resource
    private StudentCurriculumVitaeApi studentCurriculumVitaeApi;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private WebStudentService webStudentService;

    private static final String REDIS_VITAE="STUDENT_CURRICULUM_VITAE:";

    public boolean getExistRedis(Long studentId){
        synchronized(this){
            String sKey=REDIS_VITAE+studentId;
            if(stringRedisTemplate.hasKey(sKey)){
                return true;
            }else{
                stringRedisTemplate.opsForValue().set(sKey, "1", 3, TimeUnit.SECONDS);
                return false;
            }
        }
    }
    private  static final String student="{\n" +
            "    \"studentPictureUrl\": \"https://demo-oss.oss-cn-beijing.aliyuncs.com/png/860518f0dcfb42c38f0abd2dbdaeba41.png\",\n" +
            "    \"birthday\": \"2000-01-01\",\n" +
            "    \"jobStatus\": \"离职\",\n" +
            "    \"inJobTime\": \"随时入职\",\n" +
            "    \"id\": 141,\n" +
            "    \"clan\": \"汉族\",\n" +
            "    \"email\": \"423242122@qq.com\",\n" +
            "    \"liveCity\": \"北京市\",\n" +
            "    \"nickname\": \"张三\",\n" +
            "    \"nativePlace\": \"北京市北京市\",\n" +
            "    \"status\": \"0\",\n" +
            "    \"sex\": \"男\",\n" +
            "    \"graduateTime\": \"\",\n" +
            "    \"mobile\": \"18824236619\",\n" +
            "    \"studentId\": 141,\n" +
            "    \"studentName\": \"张三\",\n" +
            "    \"phoneNumber\": \"18824236619\",\n" +
            "    \"identify\": \"0\",\n" +
            "    \"publicStatus\": \"0\",\n" +
            "    \"political\": \"群众\",\n" +
            "    \"internshipDesire\": \"[{\\\"jobName\\\":\\\"产品经理\\\",\\\"expectedIndustry\\\":[\\\"铝业\\\",\\\"\\\",\\\"\\\"],\\\"targetCity\\\":\\\"北京市\\\",\\\"lowestSalary\\\":100,\\\"highestSalary\\\":200}]\",\n" +
            "    \"educationalExperience\": [\n" +
            "        {\n" +
            "            \"schoolName\": \"香港中文大学（深圳）\",\n" +
            "            \"educationalBackground\": \"本科\",\n" +
            "            \"fullTime\": \"1\",\n" +
            "            \"diploma\": \"1\",\n" +
            "            \"major\": \"金融\",\n" +
            "            \"graduateTime\": \"\",\n" +
            "            \"intakeTime\": \"2023年01月\",\n" +
            "            \"grade\": 6,\n" +
            "            \"isOverseasStudy\": \"无\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    private static final String vitae="{\n" +
            "    \"studentPictureUrl\": \"https://demo-oss.oss-cn-beijing.aliyuncs.com/png/860518f0dcfb42c38f0abd2dbdaeba41.png\",\n" +
            "    \"birthday\": \"2000-01-01\",\n" +
            "    \"jobStatus\": \"离职\",\n" +
            "    \"inJobTime\": \"随时入职\",\n" +
            "    \"id\": 141,\n" +
            "    \"clan\": \"汉族\",\n" +
            "    \"email\": \"423242122@qq.com\",\n" +
            "    \"liveCity\": \"北京市\",\n" +
            "    \"nickname\": \"张三\",\n" +
            "    \"nativePlace\": \"北京市北京市\",\n" +
            "    \"status\": \"0\",\n" +
            "    \"sex\": \"男\",\n" +
            "    \"graduateTime\": \"\",\n" +
            "    \"mobile\": \"18824236619\",\n" +
            "    \"studentId\": 141,\n" +
            "    \"studentName\": \"张三\",\n" +
            "    \"phoneNumber\": \"18824236619\",\n" +
            "    \"identify\": \"0\",\n" +
            "    \"publicStatus\": \"0\",\n" +
            "    \"political\": \"群众\",\n" +
            "    \"internshipDesire\": \"[{\\\"jobName\\\":\\\"产品经理\\\",\\\"expectedIndustry\\\":[\\\"铝业\\\",\\\"\\\",\\\"\\\"],\\\"targetCity\\\":\\\"北京市\\\",\\\"lowestSalary\\\":100,\\\"highestSalary\\\":200}]\",\n" +
            "    \"educationalExperience\": [\n" +
            "        {\n" +
            "            \"schoolName\": \"香港中文大学（深圳）\",\n" +
            "            \"educationalBackground\": \"本科\",\n" +
            "            \"fullTime\": \"1\",\n" +
            "            \"diploma\": \"1\",\n" +
            "            \"major\": \"金融\",\n" +
            "            \"graduateTime\": \"\",\n" +
            "            \"intakeTime\": \"2023年01月\",\n" +
            "            \"grade\": 6,\n" +
            "            \"isOverseasStudy\": \"无\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"name\": \"模板1\"\n" +
            "}";


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "pageNo", value = "pageNo", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(paramType = "query",name = "pageSize",value = "pageSize", dataTypeClass =Integer.class,required = true),
    })
    @GetMapping("/getImportStudentInfo")
    @ApiOperation("导入学生初始化数据")
    public CommonResult<Integer> getImportStudentInfo(Integer pageNo,Integer pageSize) {
        if(null==pageNo){pageNo=1;}
        if(null==pageSize){pageSize=5;}
        List<WebStudentInfoRespVO> studentInfoList = webStudentService.getStudentInfoList(pageNo,  pageSize);
        int i=1;
        for (WebStudentInfoRespVO s : studentInfoList) {
            long c = webStudentService.getCountByMobile(s.getMobile());
            if(0==c){
                WebStudentCreateReqVO updateReqVO = JsonUtils.parseObject(student, WebStudentCreateReqVO.class);
                updateReqVO.setNickname(s.getStudentName());
                updateReqVO.setStudentName(s.getStudentName());
                updateReqVO.setMobile(s.getMobile());
                updateReqVO.setEmail(s.getEmail());
                updateReqVO.setUniversityName(s.getUniversityName());
                updateReqVO.setSpecialty(s.getSpecialty());
                updateReqVO.setNativePlace(s.getNativePlace());
                updateReqVO.setSex(s.getSex());
                if(null==updateReqVO.getStudentAbroad()){
                    updateReqVO.setStudentAbroad(1);
                }
                Long stuId = webStudentService.createWebStudent(updateReqVO);
                StudentCurriculumVitaeCreateReqDTO stu=JsonUtils.parseObject(vitae, StudentCurriculumVitaeCreateReqDTO.class);
                stu.setStudentId(stuId);
                stu.setStudentName(s.getStudentName());
                stu.setPhoneNumber(s.getMobile());
                stu.setEmail(s.getEmail());
                stu.setUniversityName(s.getUniversityName());
                stu.setSpecialty(s.getSpecialty());
                stu.setNativePlace(s.getNativePlace());
                stu.setSex(s.getSex());
                List<StudentEducationalExperienceCreateReqDTO> educationalExperience = stu.getEducationalExperience();
                StudentEducationalExperienceCreateReqDTO f = educationalExperience.get(0);
                f.setSchoolName(s.getUniversityName());
                f.setMajor(s.getSpecialty());
                studentCurriculumVitaeApi.createStudentCurriculumVitae(stu);
            }
            i++;
        }
        return success(i);
    }

    @PostMapping("/create")
    @ApiOperation("创建用户简历")
    public CommonResult<Long> createStudentCurriculumVitae(@Valid @RequestBody StudentCurriculumVitaeCreateReqDTO createReqDTO) {
        if(getExistRedis(createReqDTO.getStudentId())){
            log.info("调用创建简历重复,学生端调用,参数:"+SecurityFrameworkUtils.getLoginUserId()+","+createReqDTO.getStudentId());
            return error("操作繁忙,请稍后重试");
        }else{
            log.info("创建简历,学生端调用,参数:"+SecurityFrameworkUtils.getLoginUserId()+","+createReqDTO.getStudentId());
        }
        return success(studentCurriculumVitaeApi.createStudentCurriculumVitae(createReqDTO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户简历")
    public CommonResult<Boolean> updateStudentCurriculumVitae(@Valid @RequestBody StudentCurriculumVitaeUpdateReqDTO updateReqDTO) {
        studentCurriculumVitaeApi.updateStudentCurriculumVitae(updateReqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户简历")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteStudentCurriculumVitae(@RequestParam("id") Long id) {
        studentCurriculumVitaeApi.deleteStudentCurriculumVitae(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户简历")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<StudentCurriculumVitaeRespDTO> getStudentCurriculumVitae(@RequestParam("id") Long id) {
        StudentCurriculumVitaeRespDTO studentCurriculumVitae = studentCurriculumVitaeApi.getStudentCurriculumVitae(id);
        return success(studentCurriculumVitae);
    }

    @GetMapping("/getStudentCurriculumVitaeByStudentId")
    @ApiOperation("获得用户简历")
    public CommonResult<List<StudentCurriculumVitaeRespDTO>> getStudentCurriculumVitaeByStudentId(@RequestParam("studentId") Long studentId) {
        List<StudentCurriculumVitaeRespDTO> studentCurriculumVitae = studentCurriculumVitaeApi.getStudentCurriculumVitaeByStudentId(studentId);
        return success(studentCurriculumVitae);
    }



    @GetMapping("/page")
    @ApiOperation("获得用户简历分页")
    public CommonResult<PageResult<StudentCurriculumVitaeRespDTO>> getStudentCurriculumVitaePage(@Valid StudentCurriculumVitaePageReqDTO pageDTO) {
        PageResult<StudentCurriculumVitaeRespDTO> pageResult = studentCurriculumVitaeApi.getStudentCurriculumVitaePage(pageDTO);
        return success(pageResult);
    }

    @GetMapping("/getStudentCurriculumVitaeBylogin")
    @ApiOperation("获得用户简历")
    public CommonResult<List<StudentCurriculumVitaeRespDTO>> getStudentCurriculumVitaeBylogin() {
        List<StudentCurriculumVitaeRespDTO> studentCurriculumVitae = studentCurriculumVitaeApi.getStudentCurriculumVitaeByStudentId( SecurityFrameworkUtils.getLoginUserId());
        return success(studentCurriculumVitae);
    }

    @GetMapping("/getStudentInfoByMobile")
    @ApiOperation("获得学生信息")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, example = "1024", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, example = "1024", dataTypeClass = String.class)
    })
    @PreAuthenticated
    public CommonResult<StudentCurriculumVitaeRespDTO> getStudentInfoByMobile(@RequestParam("projectId") Long projectId,@RequestParam("mobile")  String mobile) {
        StudentCurriculumVitaeRespDTO studentCurriculumVitae = studentCurriculumVitaeApi.getStudentInfoByMobile(projectId,mobile);
        return success(studentCurriculumVitae);
    }
}
